﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Pipes.Queries.Model.Query
{
  public class DateLiteral : IExpression
  {
    public bool Deterministic { get { return true; } }
    public MetaData.GeneralDataType InferredType { get; set; }
    public DateTime Value { get; set; }

    public DateLiteral() 
    {
      this.InferredType = MetaData.GeneralDataType.date;
    }
    public DateLiteral(DateTime value) : this()
    {
      this.Value = value;
    }
    private static string[] _formats = { "M/d/yyyy h:mm:sstt", "M/d/yyyy h:mm:ss tt", "M/d/yyyy H:mm:ss", "M/d/yyyy h:mmtt", "M/d/yyyy h:mm tt", "M/d/yyyy H:mm", "M/d/yyyy htt", "M/d/yyyy h tt", "M/d/yyyy H", "M-d-yyyy h:mm:sstt", "M-d-yyyy h:mm:ss tt", "M-d-yyyy H:mm:ss", "M-d-yyyy h:mmtt", "M-d-yyyy h:mm tt", "M-d-yyyy H:mm", "M-d-yyyy htt", "M-d-yyyy h tt", "M-d-yyyy H", "M/d/yyyy", "M-d-yyyy", "M/d/yy h:mm:sstt", "M/d/yy h:mm:ss tt", "M/d/yy H:mm:ss", "M/d/yy h:mmtt", "M/d/yy h:mm tt", "M/d/yy H:mm", "M/d/yy htt", "M/d/yy h tt", "M/d/yy H", "M-d-yy h:mm:sstt", "M-d-yy h:mm:ss tt", "M-d-yy H:mm:ss", "M-d-yy h:mmtt", "M-d-yy h:mm tt", "M-d-yy H:mm", "M-d-yy htt", "M-d-yy h tt", "M-d-yy H", "M/d/yy", "M-d-yy", "h:mm:sstt", "h:mm:ss tt", "H:mm:ss", "h:mmtt", "h:mm tt", "H:mm", "htt", "h tt", "H" };

    public DateLiteral(string value)
    {
      this.Value = DateTime.ParseExact(value.Trim('#').Trim(), _formats, new CultureInfo("en-US"), DateTimeStyles.NoCurrentDateDefault);
    }

    public override string ToString()
    {
      return this.Value.ToString();
    }

    public void Visit(Parser.IQueryVisitor visitor)
    {
      visitor.Visit(this);
    }

    public object Evaluate()
    {
      return this.Value;
    }

    public static implicit operator DateTime(DateLiteral x)
    {
      return x.Value;
    }
  }
}
